
package com.meisiabc.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Description:日期工具类
 * @Date：2017年6月27日 @author： DongY
 * @version ：1.0 1.[2017年6月27日]CreatBy：dy
 */

public class DateUtil {
	private static Calendar calendar = new GregorianCalendar();

	/**
	 * @Description: 获取当前年份
	 * @return int 返回类型
	 */
	public static int getCurrentYear() {
		final int year = calendar.get(Calendar.YEAR);
		return year;
	}

	/**
	 * @Description: 根据当前时间获取编报年，规则：6月份以前取当前年，6月份以后取下一年
	 * @return int
	 */
	public static int getEditYearByCurrent() {
		int year = getCurrentYear();
		return getCurrentMonth() > 6 ? ++year : year;
	}

	/**
	 * @Description: 获取当前月份
	 * @return int 返回类型
	 */
	public static int getCurrentMonth() {
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * @Description: 获取下个月的月份
	 * @return int 返回类型
	 */
	public static int getNextMonth() {
		return (calendar.get(Calendar.MONTH) == 11) ? 1 : calendar.get(Calendar.MONTH) + 2;
	}

	/**
	 * @Description: 获取上个月的月份
	 * @return int 返回类型
	 */
	public static int getLastMonth() {
		return (calendar.get(Calendar.MONTH) == 0) ? 12 : calendar.get(Calendar.MONTH);
	}

	/**
	 * @Description: 获取下个月所属的年份
	 * @return int 返回类型
	 */
	public static int getYearOfTheNextMonth() {
		return getCurrentMonth() == 12 ? getCurrentYear() + 1 : getCurrentYear();
	}

	/**
	 * @Description: 获取当前日
	 * @return int 返回类型
	 */
	public static int getCurrentDay() {
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * @Description: 获取上个月所属年
	 * @return int 返回类型
	 */
	public static int getYearOfTheLastMonth() {
		return getCurrentMonth() == 1 ? getCurrentYear() - 1 : getCurrentYear();
	}

	/**
	 * 获取系统当前时间年月日时分秒
	 */

	public static String getDate() {
		final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		return df.format(new Date());// new Date()为获取当前系统时间
	}

	public static Date getNextWeek() {
		final Calendar c = Calendar.getInstance();
		c.add(getCurrentDay(), 7);
		System.out.println(c.getTime());
		return c.getTime();
	}

	public static Date str2Date(final String dateStr) throws ParseException {
		final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		return df.parse(dateStr);
	}

	public static Date str2Date(final String dateStr,String pattern) throws ParseException {
		final SimpleDateFormat df = new SimpleDateFormat(pattern);// 设置日期格式
		return df.parse(dateStr);
	}


	public static void main(final String[] args) throws ParseException {
		// final String string = "2018-10-15";
		// System.out.println(getWeekByDate(str2Date(string)));
		System.out.println("=============================");
		System.out.println(getNextWeekDate(str2Date("2018-10-16"), 2));
	}

	public static int dayInWeek(final Date date) {
		final Calendar c = Calendar.getInstance();
		// c.setFirstDayOfWeek(Calendar.MONDAY);
		System.err.println(c.getMinimalDaysInFirstWeek());
		c.setTime(date);
		int i = c.get(Calendar.DAY_OF_WEEK);
		i = i == 0 ? 7 : i - 1;
		return i;
	}

	/**
	 * 获取下一个周一的日期
	 */
	public static Date getNextMondayDate(final Date date) {
		System.out.println(date);
		int day = dayInWeek(date);
		if (day == 2) {
			return date;
		} else {
			day = day == 0 ? 7 : day;
			day = 9 - day;
			System.out.println(day);
			final Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DATE, day);
			System.out.println(defalultFomrt(c.getTime()));
			return c.getTime();
		}
	}

	public static String defalultFomrt(final Date date) {
		final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		return df.format(date);
	}


	public static String defalultFomrt(final Date date,String pattern) throws Exception {
		final SimpleDateFormat df = new SimpleDateFormat(pattern);// 设置日期格式
		return df.format(date);
	}

	/**
	 * 
	 * @Description:
	 * @param date
	 *            从该日期后查询
	 * @param days
	 *            周几
	 * @return Date
	 */
	public static Date getNextWeekDate(final Date date, int days) {
		final int day = dayInWeek(date);
		// day = day == 0 ? 7 : day - 1;
		if (day == days) {
			return date;
		} else {
			days = days > day ? days - day : days + 7 - day;
			// day = day == 0 ? 7 : day;
			// day = 9 - day;
			// System.out.println(day);
			final Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DATE, days);
			System.out.println(defalultFomrt(c.getTime()));
			return c.getTime();
		}
	}

	public static Date addDay(final Date date, final int days) {
		final Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, days);
		return c.getTime();
	}

	public static String addDayStr(final Date date, final int days) {
		final Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, days);
		return defalultFomrt(c.getTime());
	}

	public static int hoursDiff(String date1, String date2) throws Exception {
		SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH");//如2016-08-10 20:40
		long from = simpleFormat.parse(date1).getTime();
		long to = simpleFormat.parse(date2).getTime();
		int hours = (int) ((to - from) / (1000 * 60 * 60));
		return hours;
	}
}
